<?php
/**
 * Created by PhpStorm.
 * User: l
 * Date: 2016/9/9
 * Time: 11:30
 */
namespace app\models;

use yii\db\ActiveRecord;
class Admin_admin extends ActiveRecord{

    public function getBar($admin_id){
        $admin = Admin_admin::find()->where([
            'admin_id'=>$admin_id,
        ])->asArray()->one();
        return $admin;
    }

    public function getUserById($admin_id){
        $admin = Admin_admin::find()->where([
            'bar_id'=>$admin_id,
        ])->asArray()->one();

        return $admin;
    }

    public function showAdmin(){
        $result = Admin_admin::find()->asArray()->all();

        return $result;
    }

    // 查找员工数据
    public function findAdmin($limit, $offset){
        $post = Admin_admin::find()->where(['<>', 'admin_id', '0']);
        $count = $post->count();
        $result = $post->limit($limit)->offset($offset)->asArray()->all();
        
        return ['total' => $count, 'rows' => $result];
    }

    public static function findByUsername($mobile,$md5Password)
    {
        $admin = Admin_admin::find()
            ->where([
                'mobile' => $mobile,
                'password'=>$md5Password
            ])->one();
        if($admin){
            return new static($admin);
        }
        return null;
    }

    public function changePasswordById($admin_id,$md5password){

        $result=\Yii::$app->db->createCommand()->update('admin_admin', [
            'password' => $md5password], ['admin_id'=>$admin_id])->execute();

        return $result;
    }

    public function updateContent($content){
        $admin = Admin_admin::findOne(0);
        $admin->update_content = $content;

        return $admin->save();
    }

    public function updateImage($img){
        $admin = Admin_admin::findOne(0);
        $admin->LoadingScreenUrl = $img;

        return $admin->save();
    }

    public function getPictureCount(){
        $count = Admin_admin::find()->select('gift_shareCount')->where([
            'admin_id'=>0,
        ])->one();

        return $count;
    }

    public function updatePictureCount($count){
        $admin = Admin_admin::findOne(0);
        $admin->gift_shareCount = $admin->gift_shareCount + $count;

        return $admin->save();
    }

    //    修改员工状态
    public function editAdmin($adminId, $status, $pwd, $type) {
        $admin = Admin_admin::findOne(['admin_id' => $adminId]);
        if ($status != -1) {
            $admin->is_deleted = $status;
        }

        if ($pwd != -1) {
            $admin->password = $pwd;
        }

        if ($type != -1) {
            $admin->type = $type;
        }

        return $admin->save();
    }

    // 查找员工信息
    public function findStaff($adminId) {
        $admin = Admin_admin::find()->where(['admin_id' => $adminId])->asArray()->one();

        return $admin;
    }

    // 设置广告图
    public function setBanner($adminId, $bannerPic){
        $admin = Admin_admin::findOne(['admin_id' => $adminId]);

        $admin->LoadingScreenUrl = $bannerPic;
        
        $result = $admin->save();
        return $result;
    }
}